/*
write by wuweiwei(邬畏畏)
www.flybirdsoft.com/wing
www.github.com/flybirdsoft
www.flybirdsoft.com/WUI
*/
(function(){
if(Array.prototype.indexOf==undefined)
{
	Array.prototype.indexOf = function(v){
		var i;
		for(i=0;i<this.length;i++)
		{
			if(this[i]==v)
			{
				return i;
			}
		}
		return -1;
	}
}


var wDatePicker = function(options){
	/*
	options={
		container:DOM,
		name:"",
		dateHeight:100 //日历控件日期区域的高度,可选参数
		date:"2018-6-12",         可选,默认当前时间
		onclick:function(e,v){},
		arrowLeftText:"<",
		arrowRightText:">",
		disableDay:[20,25],       可选
		enableDay:[20,23,24,25],  可选,enableDay优先于disableDay
		startDate:"2018-6-10",    可选,startDate,endDate 是日历控件选择月份范围
		endDate:"2018-6-30",      可选,
		usebleDate:{              可选,usebleDate 优先于 enableDay和disableDay
			beginDate:"",
			endDate:""
		},
		onprevclick:function(e,v){},
		onnextclick:function(e,v){},
		width:n,
		height:n,
		gap:n                    如果日历区域超出范围,可以通过gap修正
	}
	*/

	var wDate;
	this.options = options;
	this.DOM = {};
	this.currentSpan = null;/*当前选中的SPAN即日期*/
	this.currentMonth = ""; /*用于用户点击切换月*/
	this.currentYear = "";  /*用于用户点击切换年*/

	if(this.options.date==undefined)
	{
		wDate = new Date();
		this.options.date = wDate.getFullYear() + "-" + (wDate.getMonth()+1) + "-" + wDate.getDate();
	}

	this.options.date = this.convertDate(this.options.date);

	this.init();
	this.initDate(this.options.date);
	this.bindEvent();
};

wDatePicker.prototype.convertDate = function(dateStr){
	if(navigator.userAgent.indexOf("Trident")>0)
	{
		return dateStr.replace(/-/g,"/");
	}
	else
	{
		return dateStr;
	}
};

wDatePicker.prototype.init = function(){
	this.width = this.options.width||this.options.container.offsetWidth;
	this.height = this.options.height||this.options.container.offsetHeight;

	this.DOM.container = document.createElement("div");
	this.DOM.container.id = this.options.name||"wDatePicker";
	this.DOM.container.className = "wDatePicker";
	this.DOM.container.style.position = "relative";
	this.DOM.container.style.width = this.width+"px";
	this.DOM.container.style.height = this.height+"px";

	this.DOM.titleBar = document.createElement("div");
	this.DOM.titleBar.className = "wdp-titleBar";

	this.DOM.titleBar_arrowA = document.createElement("div");
	this.DOM.titleBar_arrowA.className = "wdp-titleBar-arrow wdp-titleBar-l-arrow";
	if(this.options.arrowLeftText==undefined)
	{
		this.DOM.titleBar_arrowA.innerHTML = '<i></i>';
	}
	else
	{
		this.DOM.titleBar_arrowA.innerHTML = '<i>'+this.options.arrowLeftText+'</i>';
	}
	this.DOM.titleBar_arrowB = document.createElement("div");
	this.DOM.titleBar_arrowB.className = "wdp-titleBar-arrow wdp-titleBar-r-arrow";
	if(this.options.arrowRightText==undefined)
	{
		this.DOM.titleBar_arrowB.innerHTML = '<i></i>';
	}
	else
	{
		this.DOM.titleBar_arrowB.innerHTML = '<i>'+this.options.arrowRightText+'</i>';
	}

	this.DOM.titleBar_date = document.createElement("div");
	this.DOM.titleBar_date.className = "wdp-titleBar-date";

	this.DOM.dateContainer = document.createElement("div");
	this.DOM.dateContainer.className = "wdp-dc";

	this.DOM.dateContainer_up = document.createElement("div");
	this.DOM.dateContainer_up.className = "wdp-dc-up";
	var weekHtml = '<span style="{style}">日</span><span style="{style}">一</span><span style="{style}">二</span>\
					<span style="{style}">三</span><span style="{style}">四</span><span style="{style}">五</span>\
					<span style="{style}">六</span>';
	var marginHarf;
	mHarf = this.width/7;
	weekHtml = weekHtml.replace(/{style}/g,"display:block;float:left;text-align:center;width:"+mHarf+"px");
	this.DOM.dateContainer_up.innerHTML = weekHtml;

	this.DOM.dateContainer_down = document.createElement("div");
	this.DOM.dateContainer_down.className = "wdp-dc-down";

	this.DOM.titleBar.appendChild(this.DOM.titleBar_arrowA);
	this.DOM.titleBar.appendChild(this.DOM.titleBar_date);
	this.DOM.titleBar.appendChild(this.DOM.titleBar_arrowB);

	this.DOM.dateContainer.appendChild(this.DOM.dateContainer_up);
	this.DOM.dateContainer.appendChild(this.DOM.dateContainer_down);

	this.DOM.container.appendChild(this.DOM.titleBar);
	this.DOM.container.appendChild(this.DOM.dateContainer);
	this.options.container.appendChild(this.DOM.container);
};

/*先不用*/
wDatePicker.prototype.createHtmlDate = function(date,weekHtmlTpl){
	var usebleDateA,usebleDateB,usebleYearA,usebleMonthA,usebleDayA,usebleYearB,usebleMonthB,usebleDayB,condition;
	var conditionType ,bool;
	var options = this.options;
	var weekHtml;
	weekHtml = weekHtmlTpl;
	weekHtml = weekHtml.replace("{style}","width:"+spanWidth+"px;height:"+spanHeight+"px;line-height:"+spanHeight+"px");
	weekHtml = weekHtml.replace("{number}",i);

	if(date!=undefined)
	{
		date = new Date(date);
	}
	else
	{
		date = new Date();
	}
	year = parseInt(date.getFullYear());
	month = date.getMonth()+1;
	curDate = date.getDate();
	
	if(options.usebleDate!=undefined)
	{
		usebleDateA = new Date(options.usebleDate.beginDate);
		usebleDateB = new Date(options.usebleDate.endDate);
		usebleYearA = usebleDateA.getFullYear();
		usebleMonthA = usebleDateA.getMonth()+1;
		usebleDayA = usebleDateA.getDate();
		usebleYearB = usebleDateB.getFullYear();
		usebleMonthB = usebleDateB.getMonth()+1;
		usebleDayB = usebleDateB.getDate();

		conditionType = usebleMonthA == usebleMonthB;
		condition = this.currentMonth==usebleMonthA;
		bool = (this.currentMonth==usebleMonthA&&i>=usebleDayA);
		bool = bool || (this.currentMonth==usebleMonthB&&i<=usebleDayB);
		
		if(conditionType&&condition&&i>=usebleDayA&&i<=usebleDayB)
		{
			if(i==parseInt(curDate))
			{
				weekHtml = weekHtml.replace("{class}","wdp-dc-normal wdp-dc-current");
			}
			else
			{
				weekHtml = weekHtml.replace("{class}","wdp-dc-normal");
			}
		}
		else if((!conditionType)&&bool)
		{
			if(i==parseInt(curDate))
			{
				weekHtml = weekHtml.replace("{class}","wdp-dc-normal wdp-dc-current");
			}
			else
			{
				weekHtml = weekHtml.replace("{class}","wdp-dc-normal");
			}					
		}

		if(i==parseInt(curDate))
		{
			weekHtml = weekHtml.replace("{class}","wdp-dc-normal wdp-dc-current");
		}
		else
		{
			weekHtml = weekHtml.replace("{class}","wdp-dc-fail");
		}

	}
	else if(options.enableDay!=undefined)
	{
		if(options.enableDay.indexOf(i)>=0)
		{
			if(i==parseInt(curDate))
			{
				weekHtml = weekHtml.replace("{class}","wdp-dc-normal wdp-dc-current");
			}
			else
			{
				weekHtml = weekHtml.replace("{class}","wdp-dc-normal");
			}
		}
		else if(i==parseInt(curDate))
		{
			weekHtml = weekHtml.replace("{class}","wdp-dc-normal wdp-dc-current");
		}
		else
		{
			weekHtml = weekHtml.replace("{class}","wdp-dc-fail");
		}
	}
	else if(options.disableDay!=undefined)
	{
		if(options.disableDay.indexOf(i))
		{
			weekHtml = weekHtml.replace("{class}","wdp-dc-fail");
		}
		else if(i==parseInt(curDate))
		{
			weekHtml = weekHtml.replace("{class}","wdp-dc-normal wdp-dc-current");
		}
		else
		{
			weekHtml = weekHtml.replace("{class}","wdp-dc-normal");
		}				
	}

	return weekHtml;
}

wDatePicker.prototype.isCurDay = function(_day_){
	var wDate,year,month,day;
	wDate = new Date(this.options.date);
	year = wDate.getFullYear();
	month = wDate.getMonth()+1;
	day = wDate.getDate();
	if(this.currentYear==year&&this.currentMonth==month&&_day_==day)
	{
		return true;
	}
	else
	{
		return false;
	}
}

wDatePicker.prototype.isRun = function(year){
	/*是否闰年*/
	if(year%400==0||year%4==0&&year%100!=0)
	{
		return true;
	}
	else
	{
		return false;
	}
}

wDatePicker.prototype.initDate = function(date){
	/*
	params.startDay, 当月开始日
	params.endDay    当月结束日
	用户参数:startDate,endDate中的日
	*/
	var weekHeight = this.options.dateHeight||(this.height-this.DOM.titleBar.offsetHeight-this.DOM.dateContainer_up.offsetHeight);
	var weekHtmls="",weekHtml,weekHtmlTpl = '<span class="{class}" style="display:block;float:left;text-align:center;{style}">{number}</span>';
	var spanWidth,spanHeight;
	var week,month,year,curDate;
	//存储useableDate.beginDate,useableDate.endDate
	var usebleDateA,usebleDateB,usebleYearA,usebleMonthA,usebleDayA,usebleYearB,usebleMonthB,usebleDayB,condition;
	var conditionType ,bool,bool2,bool3;
	var options = this.options;
	if(this.options.gap!=undefined)
	{
		weekHeight = weekHeight - this.options.gap;
	}

	if(date!=undefined)
	{
		date = new Date(date);
	}
	else
	{
		date = new Date();
	}
	year = parseInt(date.getFullYear());
	month = date.getMonth()+1;
	curDay = date.getDate();

	this.currentYear = year;
	this.currentMonth = month;

	date = new Date(this.convertDate(year.toString()+"-"+month.toString()+"-01"));
	week = parseInt(date.getDay());
	spanWidth = this.width/7;
	spanHeight = weekHeight/6;
	
	var i;
	var n,n2;
	var isRun = false , isRunN=0;

	this.DOM.titleBar_date.innerHTML = year.toString() +"年"+ month.toString() + "月";
	/*是否闰年*/
	if(year%400==0||year%4==0&&year%100!=0)
	{
		isRun = true;
	}

	if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
	{
		if(isRun&&month-1==2)
		{
			n = 28-week+1;
		}
		else if(!isRun&&month-1==2)
		{
			n = 29-week+1;
		}
		else if(month-1==7)
		{
			n = 31 - week +1;
		}
		else
		{
			n = 30 -week +1;
		}

		for(i=0;i<week;i++)
		{
			weekHtml = weekHtmlTpl;
			weekHtml = weekHtml.replace("{style}","width:"+spanWidth+"px;height:"+spanHeight+"px;line-height:"+spanHeight+"px");
			weekHtml = weekHtml.replace("{number}",n++);
			weekHtml = weekHtml.replace("{class}","wdp-dc-fail");
			weekHtmls+=weekHtml;
		}
		for(i=1;i<=31;i++)
		{
			weekHtml = weekHtmlTpl;
			weekHtml = weekHtml.replace("{style}","width:"+spanWidth+"px;height:"+spanHeight+"px;line-height:"+spanHeight+"px");
			weekHtml = weekHtml.replace("{number}",i);

			if(options.usebleDate!=undefined)
			{
				usebleDateA = new Date(options.usebleDate.beginDate);
				usebleDateB = new Date(options.usebleDate.endDate);
				usebleYearA = usebleDateA.getFullYear();
				usebleMonthA = usebleDateA.getMonth()+1;
				usebleDayA = usebleDateA.getDate();
				usebleYearB = usebleDateB.getFullYear();
				usebleMonthB = usebleDateB.getMonth()+1;
				usebleDayB = usebleDateB.getDate();

				conditionType = usebleYearA == usebleYearB && usebleMonthA == usebleMonthB;/*如果是用一个月的日期区间*/
				condition = this.currentMonth==usebleMonthA;
				bool = (this.currentYear==usebleYearA&&this.currentMonth==usebleMonthA&&i>=usebleDayA);
				bool = bool || (this.currentYear==usebleYearB&&this.currentMonth==usebleMonthB&&i<=usebleDayB);
				bool2 = this.currentYear==usebleYearA&&this.currentYear==usebleYearB&&this.currentMonth>usebleMonthA&&this.currentMonth<usebleMonthB;
				bool3 = this.currentYear>=usebleYearA&&this.currentYear<usebleYearB&&this.currentMonth>usebleMonthA;
				bool3 = bool3 || this.currentYear>usebleYearA&&this.currentYear<=usebleYearB&&this.currentMonth<usebleMonthB;
				bool = bool || bool2 || bool3 || this.currentYear>usebleYearA&&this.currentYear<usebleYearB;

				if(conditionType&&condition&&i>=usebleDayA&&i<=usebleDayB)
				{
					if(this.isCurDay(i))
					{
						weekHtml = weekHtml.replace("{class}","wdp-dc-normal wdp-dc-current");
					}
					else
					{
						weekHtml = weekHtml.replace("{class}","wdp-dc-normal");
					}
				}
				else if((!conditionType)&&bool)
				{
					if(this.isCurDay(i))
					{
						weekHtml = weekHtml.replace("{class}","wdp-dc-normal wdp-dc-current");
					}
					else
					{
						weekHtml = weekHtml.replace("{class}","wdp-dc-normal");
					}					
				}

				if(this.isCurDay(i))
				{
					weekHtml = weekHtml.replace("{class}","wdp-dc-normal wdp-dc-current");
				}
				else
				{
					weekHtml = weekHtml.replace("{class}","wdp-dc-fail wdp-dc-disable");
				}

			}
			else if(options.enableDay!=undefined)
			{
				if(options.enableDay.indexOf(i)>=0)
				{
					if(this.isCurDay(i))
					{
						weekHtml = weekHtml.replace("{class}","wdp-dc-normal wdp-dc-current");
					}
					else
					{
						weekHtml = weekHtml.replace("{class}","wdp-dc-normal");
					}
				}
				else if(this.isCurDay(i))
				{
					weekHtml = weekHtml.replace("{class}","wdp-dc-normal wdp-dc-current");
				}
				else
				{
					weekHtml = weekHtml.replace("{class}","wdp-dc-fail wdp-dc-disable");
				}
			}
			else if(options.disableDay!=undefined)
			{
				if(options.disableDay.indexOf(i)>=0)
				{
					weekHtml = weekHtml.replace("{class}","wdp-dc-fail wdp-dc-disable");
				}
				else if(this.isCurDay(i))
				{
					weekHtml = weekHtml.replace("{class}","wdp-dc-normal wdp-dc-current");
				}
				else
				{
					weekHtml = weekHtml.replace("{class}","wdp-dc-normal");
				}				
			}
			else
			{
				if(this.isCurDay(i))
				{
					weekHtml = weekHtml.replace("{class}","wdp-dc-normal wdp-dc-current");
				}
				else
				{
					weekHtml = weekHtml.replace("{class}","wdp-dc-normal");
				}
			}

			weekHtmls+=weekHtml;
		}
		n2 = 42 - week - 31;
		for(i=1;i<=n2;i++)
		{
			weekHtml = weekHtmlTpl;
			weekHtml = weekHtml.replace("{style}","width:"+spanWidth+"px;height:"+spanHeight+"px;line-height:"+spanHeight+"px");
			weekHtml = weekHtml.replace("{number}",i);
			weekHtml = weekHtml.replace("{class}","wdp-dc-fail");
			weekHtmls+=weekHtml;
		}
	}
	else if(month==2)
	{
		n = 31 -week +1;
		for(i=0;i<week;i++)
		{
			weekHtml = weekHtmlTpl;
			weekHtml = weekHtml.replace("{style}","width:"+spanWidth+"px;height:"+spanHeight+"px;line-height:"+spanHeight+"px");
			weekHtml = weekHtml.replace("{number}",n++);
			weekHtml = weekHtml.replace("{class}","wdp-dc-fail");
			weekHtmls+=weekHtml;			
		}
		if(isRun)
		{
			isRunN = 29;
		}
		else
		{
			isRunN = 28;
		}
		for(i=1;i<=isRunN;i++)
		{
			weekHtml = weekHtmlTpl;
			weekHtml = weekHtml.replace("{style}","width:"+spanWidth+"px;height:"+spanHeight+"px;line-height:"+spanHeight+"px");
			weekHtml = weekHtml.replace("{number}",i);

			if(options.usebleDate!=undefined)
			{
				usebleDateA = new Date(options.usebleDate.beginDate);
				usebleDateB = new Date(options.usebleDate.endDate);
				usebleYearA = usebleDateA.getFullYear();
				usebleMonthA = usebleDateA.getMonth()+1;
				usebleDayA = usebleDateA.getDate();
				usebleYearB = usebleDateB.getFullYear();
				usebleMonthB = usebleDateB.getMonth()+1;
				usebleDayB = usebleDateB.getDate();

				conditionType = usebleYearA == usebleYearB && usebleMonthA == usebleMonthB;/*如果是用一个月的日期区间*/
				condition = this.currentMonth==usebleMonthA;
				bool = (this.currentYear==usebleYearA&&this.currentMonth==usebleMonthA&&i>=usebleDayA);
				bool = bool || (this.currentYear==usebleYearB&&this.currentMonth==usebleMonthB&&i<=usebleDayB);
				bool2 = this.currentYear==usebleYearA&&this.currentYear==usebleYearB&&this.currentMonth>usebleMonthA&&this.currentMonth<usebleMonthB;
				bool3 = this.currentYear>=usebleYearA&&this.currentYear<usebleYearB&&this.currentMonth>usebleMonthA;
				bool3 = bool3 || this.currentYear>usebleYearA&&this.currentYear<=usebleYearB&&this.currentMonth<usebleMonthB;
				bool = bool || bool2 || bool3 || this.currentYear>usebleYearA&&this.currentYear<usebleYearB;
				
				if(conditionType&&condition&&i>=usebleDayA&&i<=usebleDayB)
				{
					if(this.isCurDay(i))
					{
						weekHtml = weekHtml.replace("{class}","wdp-dc-normal wdp-dc-current");
					}
					else
					{
						weekHtml = weekHtml.replace("{class}","wdp-dc-normal");
					}
				}
				else if((!conditionType)&&bool)
				{
					if(i==parseInt(curDate))
					{
						weekHtml = weekHtml.replace("{class}","wdp-dc-normal wdp-dc-current");
					}
					else
					{
						weekHtml = weekHtml.replace("{class}","wdp-dc-normal");
					}					
				}

				if(this.isCurDay(i))
				{
					weekHtml = weekHtml.replace("{class}","wdp-dc-normal wdp-dc-current");
				}
				else
				{
					weekHtml = weekHtml.replace("{class}","wdp-dc-fail wdp-dc-disable");
				}

			}
			else if(options.enableDay!=undefined)
			{
				if(options.enableDay.indexOf(i)>=0)
				{
					if(this.isCurDay(i))
					{
						weekHtml = weekHtml.replace("{class}","wdp-dc-normal wdp-dc-current");
					}
					else
					{
						weekHtml = weekHtml.replace("{class}","wdp-dc-normal");
					}
				}
				else if(this.isCurDay(i))
				{
					weekHtml = weekHtml.replace("{class}","wdp-dc-normal wdp-dc-current");
				}
				else
				{
					weekHtml = weekHtml.replace("{class}","wdp-dc-fail wdp-dc-disable");
				}
			}
			else if(options.disableDay!=undefined)
			{
				if(options.disableDay.indexOf(i)>=0)
				{
					weekHtml = weekHtml.replace("{class}","wdp-dc-fail wdp-dc-disable");
				}
				else if(this.isCurDay(i))
				{
					weekHtml = weekHtml.replace("{class}","wdp-dc-normal wdp-dc-current");
				}
				else
				{
					weekHtml = weekHtml.replace("{class}","wdp-dc-normal");
				}				
			}
			else
			{
				if(this.isCurDay(i))
				{
					weekHtml = weekHtml.replace("{class}","wdp-dc-normal wdp-dc-current");
				}
				else
				{
					weekHtml = weekHtml.replace("{class}","wdp-dc-normal");
				}
			}

			weekHtmls+=weekHtml;
		}

		n2 = 42 - week - isRunN +1;
		for(i=1;i<=n2;i++)
		{
			weekHtml = weekHtmlTpl;
			weekHtml = weekHtml.replace("{style}","width:"+spanWidth+"px;height:"+spanHeight+"px;line-height:"+spanHeight+"px");
			weekHtml = weekHtml.replace("{number}",i);
			weekHtml = weekHtml.replace("{class}","wdp-dc-fail");
			weekHtmls+=weekHtml;
		}
	}
	else
	{
		n = 31 -week +1;
		for(i=0;i<week;i++)
		{
			weekHtml = weekHtmlTpl;
			weekHtml = weekHtml.replace("{style}","width:"+spanWidth+"px;height:"+spanHeight+"px;line-height:"+spanHeight+"px");
			weekHtml = weekHtml.replace("{number}",n++);
			weekHtml = weekHtml.replace("{class}","wdp-dc-fail");
			weekHtmls+=weekHtml;			
		}
		for(i=1;i<=30;i++)
		{
			weekHtml = weekHtmlTpl;
			weekHtml = weekHtml.replace("{style}","width:"+spanWidth+"px;height:"+spanHeight+"px;line-height:"+spanHeight+"px");
			weekHtml = weekHtml.replace("{number}",i);

			if(options.usebleDate!=undefined)
			{
				usebleDateA = new Date(options.usebleDate.beginDate);
				usebleDateB = new Date(options.usebleDate.endDate);
				usebleYearA = usebleDateA.getFullYear();
				usebleMonthA = usebleDateA.getMonth()+1;
				usebleDayA = usebleDateA.getDate();
				usebleYearB = usebleDateB.getFullYear();
				usebleMonthB = usebleDateB.getMonth()+1;
				usebleDayB = usebleDateB.getDate();

				conditionType = usebleYearA == usebleYearB && usebleMonthA == usebleMonthB;/*如果是用一个月的日期区间*/
				condition = this.currentMonth==usebleMonthA;
				bool = (this.currentYear==usebleYearA&&this.currentMonth==usebleMonthA&&i>=usebleDayA);
				bool = bool || (this.currentYear==usebleYearB&&this.currentMonth==usebleMonthB&&i<=usebleDayB);
				bool2 = this.currentYear==usebleYearA&&this.currentYear==usebleYearB&&this.currentMonth>usebleMonthA&&this.currentMonth<usebleMonthB;
				bool3 = this.currentYear>=usebleYearA&&this.currentYear<usebleYearB&&this.currentMonth>usebleMonthA;
				bool3 = bool3 || this.currentYear>usebleYearA&&this.currentYear<=usebleYearB&&this.currentMonth<usebleMonthB;
				bool = bool || bool2 || bool3 || this.currentYear>usebleYearA&&this.currentYear<usebleYearB;
				
				if(conditionType&&condition&&i>=usebleDayA&&i<=usebleDayB)
				{
					if(this.isCurDay(i))
					{
						weekHtml = weekHtml.replace("{class}","wdp-dc-normal wdp-dc-current");
					}
					else
					{
						weekHtml = weekHtml.replace("{class}","wdp-dc-normal");
					}
				}
				else if((!conditionType)&&bool)
				{
					if(this.isCurDay(i))
					{
						weekHtml = weekHtml.replace("{class}","wdp-dc-normal wdp-dc-current");
					}
					else
					{
						weekHtml = weekHtml.replace("{class}","wdp-dc-normal");
					}					
				}

				if(this.isCurDay(i))
				{
					weekHtml = weekHtml.replace("{class}","wdp-dc-normal wdp-dc-current");
				}
				else
				{
					weekHtml = weekHtml.replace("{class}","wdp-dc-fail wdp-dc-disable");
				}

			}
			else if(options.enableDay!=undefined)
			{
				if(options.enableDay.indexOf(i)>=0)
				{
					if(this.isCurDay(i))
					{
						weekHtml = weekHtml.replace("{class}","wdp-dc-normal wdp-dc-current");
					}
					else
					{
						weekHtml = weekHtml.replace("{class}","wdp-dc-normal");
					}
				}
				else if(this.isCurDay(i))
				{
					weekHtml = weekHtml.replace("{class}","wdp-dc-normal wdp-dc-current");
				}
				else
				{
					weekHtml = weekHtml.replace("{class}","wdp-dc-fail wdp-dc-disable");
				}
			}
			else if(options.disableDay!=undefined)
			{
				if(options.disableDay.indexOf(i)>=0)
				{
					weekHtml = weekHtml.replace("{class}","wdp-dc-fail wdp-dc-disable");
				}
				else if(this.isCurDay(i))
				{
					weekHtml = weekHtml.replace("{class}","wdp-dc-normal wdp-dc-current");
				}
				else
				{
					weekHtml = weekHtml.replace("{class}","wdp-dc-normal");
				}				
			}
			else
			{
				if(this.isCurDay(i))
				{
					weekHtml = weekHtml.replace("{class}","wdp-dc-normal wdp-dc-current");
				}
				else
				{
					weekHtml = weekHtml.replace("{class}","wdp-dc-normal");
				}
			}

			weekHtmls+=weekHtml;
		}

		n2 = 42 - week - 31 +1;
		for(i=1;i<=n2;i++)
		{
			weekHtml = weekHtmlTpl;
			weekHtml = weekHtml.replace("{style}","width:"+spanWidth+"px;height:"+spanHeight+"px;line-height:"+spanHeight+"px");
			weekHtml = weekHtml.replace("{number}",i);
			weekHtml = weekHtml.replace("{class}","wdp-dc-fail");
			weekHtmls+=weekHtml;
		}
	}
	this.DOM.dateContainer_down.innerHTML = weekHtmls;
};

wDatePicker.prototype.bindEvent = function(){
	var th = this;

	this.DOM.dateContainer_down.onclick = function(e){
		var evt = e||window.event;
		var target = evt.target||evt.srcElement;
		var value;

		if(target.nodeName=="SPAN")
		{
			if(target.className.indexOf("wdp-dc-normal")==-1)
			{
				return;
			}
			if(th.currentSpan!=null)
			{
				th.currentSpan.className = th.currentSpan.className.replace(" wdp-dc-select","");
			}
			if(target.className.indexOf("wdp-dc-select")==-1)
			{
				target.className += " wdp-dc-select";
			}
			th.currentSpan = target;
			value = parseInt(target.innerHTML);
			this.date = th.currentYear + "-" + th.currentMonth + "-" + value;
			this.year = th.currentYear;
			this.month = th.currentMonth;
			this.year = value;

			if(target.className.indexOf("wdp-dc-normal")>=0)
			{
				th.options.onclick&&th.options.onclick(e,this.date);
			}
		}
	}

	this.DOM.titleBar_arrowA.onclick = function(e){
		var date = th.options.date;
		var year,month,day,date;
		var curDate;
		var opDate,opYear,opMonth,opDay;
		var options = th.options;
		var smallMonth = "4,6,9,11";
		date = new Date();
		day = date.getDate();

		if(options.startDate!=undefined)
		{
			opDate = new Date(th.convertDate(options.startDate));
			opYear = opDate.getFullYear();
			opMonth = opDate.getMonth()+1;
			opDay = opDate.getDate();
			if(th.currentYear<opYear||th.currentYear==opYear&&th.currentMonth<=opMonth)
			{
				th.options.onprevclick&&th.options.onprevclick(e,curDate);
				return;
			}
		}

		th.currentMonth--;
		if(th.currentMonth<1)
		{
			th.currentYear--;
			th.currentMonth = 12;
		}
		console.log(th.isRun(th.currentYear));
		if(!th.isRun(th.currentYear)&&th.currentMonth==2&&day==29)
		{
			day = 28;
		}
		else if(th.currentMonth==4||th.currentMonth==6||th.currentMonth==9||th.currentMonth==11)
		{
			if(day==31)
			{
				day = 30;
			}
		}
		curDate = th.convertDate(th.currentYear + "-" + th.currentMonth + "-" + day);
		th.initDate(curDate);
		th.options.onprevclick&&th.options.onprevclick(e,curDate);
	}

	this.DOM.titleBar_arrowB.onclick = function(e){
		var date = th.options.date;
		var year,month,day,date;
		var curDate;
		var options = th.options;
		date = new Date();
		year = date.getFullYear();
		day = date.getDate();

		if(options.endDate!=undefined)
		{
			opDate = new Date(th.convertDate(options.endDate));
			opYear = opDate.getFullYear();
			opMonth = opDate.getMonth()+1;
			opDay = opDate.getDate();
			if(th.currentYear>opYear||th.currentYear==opYear&&th.currentMonth>=opMonth)
			{
				th.options.onprevclick&&th.options.onprevclick(e,curDate);
				return;
			}
		}

		th.currentMonth++;
		if(th.currentMonth>12)
		{
			th.currentYear++;
			th.currentMonth = 1;
		}
		curDate = th.convertDate(th.currentYear + "-" + th.currentMonth + "-" + day);
		th.initDate(curDate);
		th.options.onprevclick&&th.options.onprevclick(e,curDate);
	}
};

/*** note: setEnableDay() 优先于() ***/
wDatePicker.prototype.setEnableDay = function(ar){
	/*
	ar is undefined is all enable day
	ar is [] is all disable day
	*/
	this.options.enableDay = ar;
	this.initDate(this.options.date);

};

wDatePicker.prototype.setDisableDay = function(ar){
	/*
	ar is undefined is all enable day
	ar is [] is all disable day
	*/
	this.options.disableDay = ar;
	this.initDate(this.options.date);
};

wDatePicker.prototype.setStartDate = function(dateStr){
	/*dateStr is "2018-5-6"*/
	this.options.startDate = dateStr;
};

wDatePicker.prototype.setEndDate = function(dateStr){
	/*dateStr is "2018-5-6"*/
	this.options.endDate = dateStr;
};

wDatePicker.prototype.setUsebleDate = function(_options_){
	/*
	_options_.beginDate,
	_options_.endDate
	*/
	var options = {};
	if(_options_==undefined)
	{
		this.options.usebleDate = undefined;
		this.initDate(this.options.date);
		return;
	}
	options.beginDate = this.convertDate(_options_.beginDate);
	options.endDate = this.convertDate(_options_.endDate);
	this.options.usebleDate = options;
	this.initDate(this.options.date);
};

window.wDatePicker = wDatePicker;
})();


/*
html:
<div id="date" style="width:400px;height:300px;"></div>

css:
.wDatePicker{
	border:1px solid #ccc;
}
.wDatePicker .wdp-titleBar-date{
	font-size: 20px;
	font-weight: bold;
	text-align: center;
}
.wDatePicker .wdp-titleBar{
	height: 40px;
	line-height: 40px;
	position: relative;
}
.wDatePicker .wdp-titleBar-l-arrow{
	float: left;
	height: 100%;
	width: 50px;
}
.wDatePicker .wdp-titleBar-date{
	position: absolute;
	left:50px;
	right: 50px;
	top:0;
	bottom:0;
}
.wDatePicker .wdp-titleBar-r-arrow{
	float: right;
	height: 100%;
	width: 50px;
}
.wDatePicker .wdp-dc{
	position: absolute;
	top:40px;
	bottom:0;
	left:0;
	right: 0;
}
.wDatePicker .wdp-dc-up{
	height: 30px;
	line-height: 30px;
}
.wDatePicker .wdp-dc-fail{
	color:#ccc;
}
.wDatePicker .wdp-dc-current{
	background-color: #6495ED;
	border-radius: 5px;
}
.wDatePicker .wdp-dc-normal{
	cursor: pointer;
	border-radius: 5px;
	position:relative;
}
.wDatePicker .wdp-dc-normal:hover{
	background-color: #87CEEB
}
.wDatePicker .wdp-dc-normal i{
	position: absolute;
	width:60%;
	height:60%;
	display:block;
	left:50%;
	top:50%;
	transform: translate(-50%,-50%);
}
.wDatePicker .wdp-dc-select{
	background-color: #87CEEB;
}
.wDatePicker .wdp-titleBar-arrow i{
	font-style: 18px;
	display: block;
	width:30px;
	height: 30px;
	margin-top:10px;
	margin-left:10px;
	cursor: pointer;
	font-style: normal;
}



js:
var date = new wDatePicker({
	container:document.getElementById("date"),
	gap:10,                   可选(高度的差值,即日历日期整体高度减去10)
	date:"2018-06-08",        可选
	arrowLeftText:"<",
	arrowRightText:">",
	disableDay:[20,25],       可选
	enableDay:[20,23,24,25],  可选,enableDay优先于disableDay
	startDate:"2018-6-10",    可选
	endDate:"2018-6-30",      可选
	onclick:function(e,date){
		console.log(date);
	},
	onprevclick:function(e,date){
		console.log(date);
	},
	onnextclick:function(e,date){
		console.log(date);
	}
});

*/


